package com.pucheng.bracelet.ui.activity.blood_pressure

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ToastUtils
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.App
import com.pucheng.bracelet.app.database.repository.BPRepository
import com.pucheng.bracelet.app.util.StatusBarUtil
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.databinding.ActivityBloodPressureBinding
import com.pucheng.bracelet.viewmodel.blood_pressure.BPModelFactory
import com.pucheng.bracelet.viewmodel.blood_pressure.BloodPressureViewModel
import kotlinx.android.synthetic.main.activity_blood_pressure.btn_start_measuring
import kotlinx.android.synthetic.main.activity_blood_pressure.layout_title
import kotlinx.android.synthetic.main.activity_blood_pressure.tb_hint
import kotlinx.android.synthetic.main.layout_blood_pressure_analysis_hint.*
import kotlinx.android.synthetic.main.layout_title.*
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.demo.app.base.BaseActivity

class BloodPressureActivity : BaseActivity<BaseViewModel, ActivityBloodPressureBinding>() {

    private val dataViewModel: BloodPressureViewModel by viewModels { BPModelFactory(
        BPRepository((application as App).database.bloodPressureDao())) }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = dataViewModel
        tv_title.text = getString(R.string.bp)
        layout_title.setBackgroundResource(R.color.blood_pressure_bg)
        StatusBarUtil.setColor(this, resources.getColor(R.color.blood_pressure_bg), 0)
        btn_start_measuring.click {
            dataViewModel.measuredBP()
        }
        ibtn_back.click {
            finish()
        }
        ibtn_right.click { startActivity(Intent(this, BPHistoryActivity::class.java)) }
        tv_active_description.text = "血压正常值：\n 90MMHG<收缩压<140MMHG\n 60MMHG<舒张压<90MMHG"
        tb_hint.setTitle("近7次血压测量趋势")
    }

    override fun layoutId(): Int = R.layout.activity_blood_pressure

    override fun createObserver() {
        super.createObserver()
        dataViewModel.apply {
            bpInfo.observe(this@BloodPressureActivity, Observer {
                mDatabind.tvDiastolicPressure.text = "${it.diastolicPressure}"
                mDatabind.tvSystolicPressure.text = "${it.systolicPressure}"
                mDatabind.tvMeasureTime.text = it.measuredDate
            })
            allDataTo7Day.observe(this@BloodPressureActivity, Observer {
                it.let {

                    tb_hint.showBarChart(it, resources.getColor(R.color.blood_pressure_bg))
                }
            })


        }
    }
}